package conf

import (
	"os"
)

type MySQL struct {
	MySQLHost     string
	MySQLPort     string
	MySQLUser     string
	MySQLPassword string
	MySQLDb       string
}

type PostgreSQL struct {
	PgHost     string
	PgPort     string
	PgUser     string
	PgPassword string
	PgDb       string
}

type Redis struct {
	RedisHost     string
	RedisPort     string
	RedisPassword string
	RedisDb       string
}

type RabbitMQ struct {
	RabbitMQUser string
	RabbitMQPass string
	RabbitMQHost string
	RabbitMQPort string
}

type Application struct {
	GinMode   string
	WebHost   string
	WebPort   string
	SecretKey string

	PostgreSQL PostgreSQL
	MySQL      MySQL
	Redis      Redis
	RabbitMQ   RabbitMQ
}

var App *Application

func InitConfig() {
	a := Application{
		GinMode: GetEnv("GIN_MODE", "debug"),

		WebHost:   GetEnv("WEB_HOST", "0.0.0.0"),
		WebPort:   GetEnv("WEB_PORT", "8090"),
		SecretKey: GetEnv("SECRET_KEY", "123456"),

		PostgreSQL: PostgreSQL{
			PgHost:     GetEnv("PG_HOST", "10.16.168.61"),
			PgPort:     GetEnv("PG_PORT", "5432"),
			PgUser:     GetEnv("PG_USER", "kady"),
			PgPassword: GetEnv("PG_PASSWORD", "123456"),
			PgDb:       GetEnv("PG_DB", "gids"),
		},

		MySQL: MySQL{
			MySQLHost:     GetEnv("MYSQL_HOST", "10.16.168.61"),
			MySQLPort:     GetEnv("MYSQL_PORT", "3306"),
			MySQLUser:     GetEnv("MYSQL_USER", "root"),
			MySQLPassword: GetEnv("MYSQL_PASSWORD", "123456"),
			MySQLDb:       GetEnv("MYSQL_DB", "gids"),
		},

		Redis: Redis{
			RedisHost:     GetEnv("REDIS_HOST", "10.16.168.61"),
			RedisPort:     GetEnv("REDIS_PORT", "6379"),
			RedisPassword: GetEnv("REDIS_PASSWORD", "123456"),
			RedisDb:       GetEnv("REDIS_DB", "5"),
		},

		RabbitMQ: RabbitMQ{
			RabbitMQUser: GetEnv("RABBITMQ_USER", "guest"),
			RabbitMQPass: GetEnv("RABBITMQ_PASS", "guest"),
			RabbitMQHost: GetEnv("RABBITMQ_HOST", "10.16.168.61"),
			RabbitMQPort: GetEnv("RABBITMQ_PORT", "5672"),
		},
	}

	App = &a
}

func GetEnv(key, defaultValue string) string {
	value := os.Getenv(key)
	if value == "" {
		return defaultValue
	}
	return value
}
